package cn.wj.school.server.manager;

import cn.wj.school.app.dto.LoginReqMsg;
import cn.wj.school.app.dto.LoginRespMsg;
import cn.wj.school.cloud.dto.Response;
import cn.wj.school.cloud.dto.SyUserDTO;
import cn.wj.school.cloud.dubbo.JteachDubboService;
import cn.wj.school.cloud.enums.OperateType;
import cn.wj.school.cloud.enums.UserType;
import cn.wj.school.common.Constants;
import cn.wj.school.common.dto.BaseMsg;
import cn.wj.school.common.enums.MsgType;
import cn.wj.school.common.manager.BaseManager;
import cn.wj.school.server.cache.ClientCacheMap;
import cn.wj.school.server.cache.UserCache;
import cn.wj.school.server.dubbo.DubboService;
import cn.wj.school.server.net.context.IoSession;
import cn.wj.school.server.net.context.SessionManager;
import cn.wj.school.server.net.utils.ChannelUtils;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 14:27 2019/1/3
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
@Slf4j
public class LoginManager implements BaseManager {
    /**
     * beanName
     *
     * @return
     */
    @Override
    public String beanName() {
        return MsgType.LOGIN.getBeanName();
    }

    /**
     * 处理Msg
     *
     * @param msg
     */
    @Override
    public void handleMsg(BaseMsg msg, Channel channel) {
        LoginReqMsg login = (LoginReqMsg) msg;
        JteachDubboService dubboService = DubboService.singletonJteachDubboService();
        Response<SyUserDTO> response = dubboService.userLogin(login.getUserName(), login.getPassword());
        IoSession session = ChannelUtils.getSessionBy(channel);
        LoginRespMsg resp = new LoginRespMsg();
        if (response.isSuccess()) {
            SyUserDTO userDTO = response.getResult();
            //校验用户
            validUser(userDTO, resp);
            if (resp.getSuccess() == Constants.FALSE) {
                SessionManager.INSTANCE.sendMsgTo(session, resp);
                return;
            }
            SessionManager.INSTANCE.registerSession(userDTO, session);
            UserCache.addUser2Online(userDTO);
            resp.setSuccess(Constants.TRUE);
            resp.setUserInfo(JSONObject.toJSONString(userDTO));
            log.info("======client" + channel.remoteAddress() + " 登录成功=====");
            SessionManager.INSTANCE.sendMsgTo(session, resp);
            //注册登录信息
            dubboService.registerLoginInfo(userDTO.getId(), OperateType.LOGIN);
        } else {
            resp.setSuccess(Constants.FALSE);
            resp.setError(response.getError());
            SessionManager.INSTANCE.sendMsgTo(session, resp);
        }
    }


    private void validUser(SyUserDTO userDTO, LoginRespMsg resp) {
        if (SessionManager.INSTANCE.getSessionBy(userDTO.getId()) != null) {
            resp.setSuccess(Constants.FALSE);
            resp.setError("用户已登录，请勿重复登录！");
            return;
        }
        // 如果是老师的话,确认这台服务器是教室端
       /* if (UserType.TEACHER.name().equals(userDTO.getType())) {
            if (SessionManager.INSTANCE.hasTeacher()) {
                resp.setSuccess(Constants.FALSE);
                resp.setError("一个域只能有一个教师端登录!");
                return;
            }
        }*/
        if (UserType.STUDENT.name().equals(userDTO.getType())) {
            if (userDTO.getPayStatus() == 0) {
                resp.setSuccess(Constants.FALSE);
                resp.setError("未交费用户不能登录!");
                return;
            }
        }

        /*if (UserType.STUDENT.name().equals(userDTO.getType())
                    && ClientCacheMap.get(ClientCacheMap.USER_TYPE) == null) {

                resp.setSuccess(Constants.FALSE);
                resp.setError("请等待老师端登录!");
                SessionManager.INSTANCE.sendMsgTo(session, resp);
                return;
            }*/
    }

    /**
     * 获取消息对象,客户端登录对象
     */
    @Override
    public BaseMsg getObject() {
        return new LoginReqMsg();
    }
}
